package com.pdstudio.carrecom.ui.fragment.appointment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.bean.CarSelectInfo;
import com.pdstudio.carrecom.bean.NewActivity;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.bean.ShowCar;
import com.pdstudio.carrecom.tools.UIHelper;
import com.pdstudio.carrecom.ui.activity.appointment.ActivityAppointDrive;
import com.pdstudio.carrecom.ui.activity.myclub.ActivityNewActivity;
import com.pdstudio.carrecom.ui.activity.myclub.ActivityNewActivity.MyOnPageChangeListener;
import com.pdstudio.carrecom.ui.adapter.ImagePagerAdapter;
import com.pdstudio.carrecom.ui.view.AutoScrollViewPager;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 预约试驾第二部
 * 类: fragmentappointdrive2 <br>
 * 描述: TODO <br>
 * 作者: xiangzhaoyu  <br>
 * 时间: 2015-6-3 下午4:29:58
 */
public class fragmentappointdrive2 extends Fragment {

	private View _view;
	private Activity _context;
	//private TextView m4s,mCarType,mSocialPrice,mOurPrice,mDesc;//车辆类型、市场价、4s价，描述
	private TextView mCarType,mDesc;//车辆类型、市场价、4s价，描述
	private CarSelectInfo showcar=null;
	private AutoScrollViewPager mViewPager;
	private List<ShowCar> nas;
	private int currentna;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		_view = inflater.inflate(R.layout.fragment_show_car, container,false);
		_context = getActivity();
		showcar=((ActivityAppointDrive)getActivity()).currentc;
		if(showcar==null||showcar.id<=0){
			UIHelper.ToastMessage(_context, "没有获取到您当前选择的车型，请重新选择！");
		}
			initialView();
			return _view;	
		
	}
	
	public void initialView(){
		//m4s=(TextView)_view.findViewById(R.id.tv_appoint_4s);
		mCarType=(TextView)_view.findViewById(R.id.tv_appoint_type);
		//mSocialPrice=(TextView)_view.findViewById(R.id.tv_appoint_oldprice);
		//mOurPrice=(TextView)_view.findViewById(R.id.tv_appoint_nowprice);
		mDesc=(TextView)_view.findViewById(R.id.tv_appoint_desc);
		mViewPager = (AutoScrollViewPager) _view.findViewById(R.id.asv_myclub);
		initData();
	}
	
	public void initData(){
		updateInfo();
		
		try {
			HttpExecuteJson http = new HttpExecuteJson(_context,
					mCarShowListener);
			Map<String, Object> rps = new HashMap<String, Object>();
			rps.put("uid", AppContext.getInstance()._userId);
			rps.put("modelId", showcar.id);
			rps.put("type", "0");
			rps.put("size", "20-40");
			http.get(ServiceHelper.ShowCar, rps);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	};
	
	/**
	 * 更新显示数据
	 * 方法: updateInfo <br>
	 * 描述: TODO <br>
	 * 作者: xiangzhaoyu  <br>
	 * 时间: 2015-7-5 下午3:56:36
	 */
	private void updateInfo(){
		if(showcar!=null&&showcar.id>0){
			//m4s.setText(showcar.address);
			ActivityAppointDrive aad=(ActivityAppointDrive)_context;
			mCarType.setText(aad.CarSelectTitle);
			//mSocialPrice.setText(showcar.price);
			//mOurPrice.setText(showcar.price);
			mDesc.setText(showcar.name);	
		}else{
			//m4s.setText("无");
			mCarType.setText("无");
			//mSocialPrice.setText("无");
			//mOurPrice.setText("无");
			mDesc.setText("无");	
		}
		
	}

	private void DoJson(String json) {
		ResultInfo edv = null;
		try {
			edv = new Gson().fromJson(json, new TypeToken<ResultInfo>() {
			}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				// 原本在initview的代码放在这下面
				try {
					nas = new Gson().fromJson(new Gson().toJson(edv.data),
							new TypeToken<List<ShowCar>>() {
							}.getType());
					if (nas != null && nas.size() > 0) {
						if (nas.get(0) != null) {
							
						}
						List<String> imageIdList = new ArrayList<String>();
						for (int i = 0; i < nas.size(); i++) {
							if (nas.get(i) != null
									&& (!nas.get(i).url.equals(null))
									&& (!nas.get(i).url.equals(""))) {
							}
							imageIdList.add(nas.get(i).url);

							mViewPager.setAdapter(new ImagePagerAdapter(_context,
									imageIdList).setInfiniteLoop(true));
							mViewPager
									.setOnPageChangeListener(new MyOnPageChangeListener());

							mViewPager.setInterval(2000);
							mViewPager.startAutoScroll();

						}

					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private httpReturnJson mCarShowListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(_context, "获取车型数据失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(_context, "获取车型数据失败");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJson(result);
		}
	};

	@Override
	public void onPause() {
		super.onPause();
		// stop auto scroll when onPause
		mViewPager.stopAutoScroll();
	}

	@Override
	public void onResume() {
		super.onResume();
		// start auto scroll when onResume
		mViewPager.startAutoScroll();
	}

	public class MyOnPageChangeListener implements
			ViewPager.OnPageChangeListener {

		@Override
		public void onPageSelected(int position) {
			try {
				int count=nas.size();
				currentna=position%count;
				
				//mCarType.setText(nas.get(currentna).title);
				mDesc.setText(nas.get(currentna).content);
				
			} catch (Exception e) {
			}
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			try {
				int count=nas.size();
				currentna=position%count;
				
				//mCarType.setText(nas.get(currentna).title);
				mDesc.setText(nas.get(currentna).content);
			} catch (Exception e) {
			}
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

	
}
